﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class BillPurchaseGoodsService
    {
        /// <summary>
        /// 查询进货单商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BillPurchaseGoods GetBillPurchaseGoods(long id)
        {
            BillPurchaseGoods purchaseGoods = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    purchaseGoods = new BillPurchaseGoods();
                    purchaseGoods.Id = id;
                    purchaseGoods = new BillPurchaseGoodsDAO().GetBillPurchaseGoods(conn, purchaseGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return purchaseGoods;
        }

        /// <summary>
        /// 查询进货商品列表
        /// </summary>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">页面大小</param>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="purchaseno">进货单号</param>
        /// <param name="barcode">商品条码</param>
        /// <param name="adminUsername">员工号</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="ascfield">升序</param>
        /// <param name="descfield">降序</param>
        /// <returns></returns>
        public DataTable GetBillPurchaseGoodsTable(int pageindex, int pagesize, String enterpriseNo, String storeNo, String purchaseno,
            String barcode, String adminUsername, String startDate, String endDate, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillPurchaseGoods purchaseGoods = new BillPurchaseGoods();
                    purchaseGoods.Pageindex = pageindex;
                    purchaseGoods.Pagesize = pagesize;
                    purchaseGoods.EnterpriseNo = enterpriseNo;
                    purchaseGoods.StoreNo = storeNo;
                    purchaseGoods.PurchaseNo = purchaseno;
                    purchaseGoods.Barcode = barcode;
                    purchaseGoods.AdminUsername = adminUsername;
                    purchaseGoods.StartDate = startDate;
                    purchaseGoods.EndDate = endDate;
                    purchaseGoods.Ascfield = ascfield;
                    purchaseGoods.Descfield = descfield;
                    dt = new BillPurchaseGoodsDAO().GetBillPurchaseGoodsTable(conn, purchaseGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询进货商品记录数
        /// </summary>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="purchaseno">进货单号</param>
        /// <param name="barcode">商品条码</param>
        /// <param name="adminUsername">员工号</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns></returns>
        public long GetBillPurchaseGoodsRecord(String enterpriseNo, String storeNo, String purchaseno, String barcode, String adminUsername,
            String startDate, String endDate)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillPurchaseGoods purchaseGoods = new BillPurchaseGoods();
                    purchaseGoods.EnterpriseNo = enterpriseNo;
                    purchaseGoods.StoreNo = storeNo;
                    purchaseGoods.PurchaseNo = purchaseno;
                    purchaseGoods.Barcode = barcode;
                    purchaseGoods.AdminUsername = adminUsername;
                    purchaseGoods.StartDate = startDate;
                    purchaseGoods.EndDate = endDate;
                    record = new BillPurchaseGoodsDAO().GetBillPurchaseGoodsRecord(conn, purchaseGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入进货商品信息
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="purchaseGoods"></param>
        /// <returns></returns>
        public long InsertBillPurchaseGoods(DbConnection conn, BillPurchaseGoods purchaseGoods)
        {
            long id = 0;
            try
            {
                id = new BillPurchaseGoodsDAO().InsertBillPurchaseGoods(conn, purchaseGoods);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 删除进货商品信息
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="purchaseno"></param>
        /// <returns></returns>
        public int DeleteBillPurchaseGoods(DbConnection conn, String purchaseno)
        {
            int count = 0;
            try
            {
                BillPurchaseGoods purchaseGoods = new BillPurchaseGoods();
                purchaseGoods.PurchaseNo = purchaseno;
                count = new BillPurchaseGoodsDAO().DeleteBillPurchaseGoods(conn, purchaseGoods);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 查询采购商品入库数据
        /// </summary>
        /// <param name="purchaseno"></param>
        /// <returns></returns>
        public DataTable GetPurchaseGoodsStorageTable(String purchaseno)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillPurchaseGoods purchaseGoods = new BillPurchaseGoods();
                    purchaseGoods.PurchaseNo = purchaseno;
                    dt = new BillPurchaseGoodsDAO().GetPurchaseGoodsStorageTable(conn, purchaseGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
